
/**
 * Module dependencies.
 */
var express = require('express');
var routes = require('./routes/managerindex');
//var partials = require('express-partials'); //新版本express layout--partials支持
var http = require('http');
var path = require('path');
var MongoStore =require('connect-mongo')(express);// session用户信息存入数据库依赖
var config=require('./config');
var flash=require('connect-flash');     //flash通知模块
var ueditor = require("ueditor");
var app = express();
//app.use(partials());

// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(flash());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.bodyParser({keepExtensions:true,uploadDir:'./public/images/poster'}));
app.use(express.cookieParser());//cookie解析中间件
app.use(express.session({       //提供会话支持
	secret:config.cookieSecret,
    cookie:{maxAge:1000*60*60*10},//10个小时
	store:new MongoStore({
		db:config.db
	})
}))
app.use(function(req,res,next){	      //注册全局变量
	var err = req.flash('error');
    var success = req.flash('success');
	res.locals.user = req.session.user;
    res.locals.auser = req.session.auser;
	res.locals.error= err.length ? err : null;
	res.locals.success = success.length ? success : null;
	next();
});

app.use(function(req,res,next){
    var url=req.originalUrl;
    console.log(url)
    if(url.indexOf('.')<0&&url!='/manager'&&url!='/manager/login'&&!req.session.auser){
        req.flash('error','你没有登陆，不能操作');
        return res.redirect('/manager')
    }
    if(req.session.auser){
        if(url=='/manager'){
            return res.redirect('/manager/main');
        }

    }
    next();
})
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}
ueditor(app,'/ueditor/ueditor.config.json','/',path.join(__dirname, 'public'),"/upload")
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
routes(app);
